package com.cv.system.service.role;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.system.controller.system.role.vo.RolePageReqVO;
import com.cv.system.controller.system.role.vo.RoleRespVO;
import com.cv.system.controller.system.role.vo.RoleSaveReqVO;
import com.cv.system.dal.dataobject.role.SystemRoleDO;
import com.cv.system.dal.dataobject.role.SystemUserRoleDO;
import com.cv.system.dal.mapper.role.SystemRoleMapper;
import com.cv.system.dal.mapper.role.SystemUserRoleMapper;
import com.cv.system.enums.role.RoleCodeEnum;
import com.cv.system.enums.role.RoleTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 角色业务层接口实现类
 * 由于角色以固定为超级管理员、管理员、用户、客服审核人员。
 * 因此，不需要自定义角色。所以直接展示系统内置角色。
 *
 * @author Charles_XDXD
 * @see RoleTypeEnum
 */
@Service
public class SystemRoleServiceImpl implements SystemRoleService {

    @Resource
    private SystemRoleMapper systemRoleMapper;

    @Resource
    private SystemUserRoleMapper systemUserRoleMapper;

    @Override
    public PageResult<RoleRespVO> getRolePage(RolePageReqVO pageReqVO) {
        return systemRoleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RoleRespVO> getRoleList() {
        List<SystemRoleDO> systemRoleDOS = systemRoleMapper.selectList();
        return BeanUtils.toBean(systemRoleDOS, RoleRespVO.class);
    }

    @Override
    public Long getUserRole(Long userId) {
        return Optional.ofNullable(systemUserRoleMapper.selectOne("user_id", userId))
                .orElse(new SystemUserRoleDO().setRoleId(0L)).getRoleId();
    }

    @Override
    public RoleRespVO getUserRoleByUserId(Long userId) {
        SystemUserRoleDO userRoleDO = systemUserRoleMapper.selectOne("user_id", userId);
        
        if (ObjUtil.isEmpty(userRoleDO)) {
            return null;
        }

        return BeanUtils.toBean(systemRoleMapper.selectById(userRoleDO.getRoleId()), RoleRespVO.class);
    }

    @Override
    public void setUserRole(RoleSaveReqVO saveReqVO) {
        Long roleId = getUserRole(saveReqVO.getUserId());

        if (!roleId.equals(0L)) {
            systemUserRoleMapper.delete("role_id", roleId.toString());
        }

        systemUserRoleMapper.insert(new SystemUserRoleDO()
                .setUserId(saveReqVO.getUserId())
                .setRoleId(saveReqVO.getRoleId()));
    }

    @Override
    public void setUserAsCommon(Long userId) {
        List<RoleRespVO> roleList = getRoleList();
        if (CollUtil.isNotEmpty(roleList) && ObjUtil.isNotEmpty(userId)) {
            for (RoleRespVO roleRespVO : roleList) {
                if (roleRespVO.getCode().equals(RoleCodeEnum.COMMON_USER.getCode())) {
                    systemUserRoleMapper.insert(new SystemUserRoleDO()
                            .setUserId(userId)
                            .setRoleId(roleRespVO.getId()));
                }
            }
        }
    }

}
